{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from scipy import io as spio\n",
    "import numpy as np\n",
    "a = np.arange(10)\n",
    "spio.savemat('a.mat',{'a':a})\n",
    "data = spio.loadmat('a.mat', struct_as_record=True)\n",
    "data['a']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "计算矩阵的行列式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy import linalg\n",
    "m = np.array([[1, 2], [3, 4]])\n",
    "linalg.det(m)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "优化和拟合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x25cea5d6c18>]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAD8CAYAAAB0IB+mAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAIABJREFUeJzt3Xl8VdW5//HPkzmQhJCREBKmhBkRCFNQqyAqTuCs1UrVXm4Ha7X1Vtve21Y72tHrr2od6nWsgtgqUhQQUCYZwkwIkIQhIxlISEhCxrN+f5xDm2KAJGfYZ3jer1deOTlnn7Oe7Jx8s7P22mu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c4FWDd3X8i8hKwrIuH3LJ4TTf2V1cL6Zz9Gi7fX13ozvd/ZpsSx8+5H1/8d93lRCQUe9i/ZYz529mPd/4DYIxZLiLPiUiCMcbtc8Z042dj5aJIc4EdxpiKsx+wcp8BFSKSYowpd3RvVXaxTQn28wxnDMJ+DrNX/KVL54ILrjhCZC1wq+OuBcC5/mNwhSuBA8aYkq4eFJG+IhJ95jb2E5f7utrWVc7qM73pHO1tAzLFPqIpDPu/wkvdXNe5FtLpvI2n9ld3vv+l2N8/YH8/rTnXHylXcZwj+AuQZ4z5wzm2GXDmXIKITMX++33CnXU52urOz2YpcK9jtM50oO5Md4YHnPM/bav2mUPn99G58mgFcJWI9Hd0wV7luK933H122pUf2EOqBGgBKoAVnR77EfbRFQeBuZ3uXw4MdNwehv0PQQHwLhDuxlpfBb5+1n0DgeWdatnt+MjF3rXh7v33BrAX2ON4s6WcXZfj62uxjwIp9FBdBdj7KXc5Pv58dl2e3F9dff/Ak9j/IAFEON4/BY730zAP7KNLsP8rv6fTfroW+PqZ9xnwoGPf7MZ+8jvb3XWd72dzVm0CPOvYp3vpNMrOzbX1wR7g/Trd5/F9hv0PTjnQ5siwB7Cf91kN5Ds+xzm2zQJe7vTc+x3vtQLgPmfq0CttlVIqQPhLl45SSqkL0MBXSqkAoYGvlFIBQgNfKaUChAa+UkoFCA18pZQKEBr4SikVIDTwlVIqQPx/4AnDTUeQAn8AAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "from scipy import optimize\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "def f(x):\n",
    "    return x**2 + 20 * np.sin(x)\n",
    "\n",
    "x = np.arange(-10, 10, 0.1)\n",
    "plt.plot(x, f(x))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用暴力穷举法计算最小值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1.42754883])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grid = (-10, 10, 0.1)\n",
    "x_min = optimize.brute(f, (grid, ))\n",
    "x_min"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Pandas"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "下面a，b，c是索引，代表每一行数据的标识\n",
    "A，B，C是列标签，代表了每一列的名称"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>x</td>\n",
       "      <td>1000</td>\n",
       "      <td>{10, 20, 30}</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>y</td>\n",
       "      <td>2000</td>\n",
       "      <td>{10, 20, 30}</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>z</td>\n",
       "      <td>3000</td>\n",
       "      <td>{10, 20, 30}</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A     B             C\n",
       "a  x  1000  {10, 20, 30}\n",
       "b  y  2000  {10, 20, 30}\n",
       "c  z  3000  {10, 20, 30}"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "data = {'A': ['x', 'y', 'z'], 'B': [1000, 2000, 3000], 'C': {10, 20, 30}}\n",
    "df = pd.DataFrame(data, index = ['a', 'b', 'c'])\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "获取df的数据\n",
    "获取df行索引\n",
    "获取df列索引（列标签）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['x' 1000 {10, 20, 30}]\n",
      " ['y' 2000 {10, 20, 30}]\n",
      " ['z' 3000 {10, 20, 30}]]\n",
      "Index(['a', 'b', 'c'], dtype='object')\n",
      "Index(['A', 'B', 'C'], dtype='object')\n"
     ]
    }
   ],
   "source": [
    "print(df.values)\n",
    "print(df.index)\n",
    "print(df.columns)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "创建的时候，如果指定了列标签，那么DataFrame的列也会按照指定的顺序进行排列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>C</th>\n",
       "      <th>B</th>\n",
       "      <th>A</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>{10, 20, 30}</td>\n",
       "      <td>1000</td>\n",
       "      <td>x</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>{10, 20, 30}</td>\n",
       "      <td>2000</td>\n",
       "      <td>y</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>{10, 20, 30}</td>\n",
       "      <td>3000</td>\n",
       "      <td>z</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              C     B  A\n",
       "a  {10, 20, 30}  1000  x\n",
       "b  {10, 20, 30}  2000  y\n",
       "c  {10, 20, 30}  3000  z"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(data, columns = ['C', 'B', 'A'], index = ['a', 'b', 'c'])\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果某列不存在，为其赋值，则会创建一个新列（可以用来添加新列）\n",
    "也可以用del来删除列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              C     B  A   D\n",
      "a  {10, 20, 30}  1000  x  10\n",
      "b  {10, 20, 30}  2000  y  10\n",
      "c  {10, 20, 30}  3000  z  10\n",
      "              C     B  A\n",
      "a  {10, 20, 30}  1000  x\n",
      "b  {10, 20, 30}  2000  y\n",
      "c  {10, 20, 30}  3000  z\n"
     ]
    }
   ],
   "source": [
    "df['D'] = 10\n",
    "print(df)\n",
    "del df['D']\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "添加行的方法：先创建一个DataFrame，然后再使用append方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Anaconda\\lib\\site-packages\\pandas\\core\\frame.py:6211: FutureWarning: Sorting because non-concatenation axis is not aligned. A future version\n",
      "of pandas will change to not sort by default.\n",
      "\n",
      "To accept the future behavior, pass 'sort=False'.\n",
      "\n",
      "To retain the current behavior and silence the warning, pass 'sort=True'.\n",
      "\n",
      "  sort=sort)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>x</td>\n",
       "      <td>1000</td>\n",
       "      <td>{10, 20, 30}</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>y</td>\n",
       "      <td>2000</td>\n",
       "      <td>{10, 20, 30}</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>z</td>\n",
       "      <td>3000</td>\n",
       "      <td>{10, 20, 30}</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>new</td>\n",
       "      <td>4000</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     A     B             C\n",
       "a    x  1000  {10, 20, 30}\n",
       "b    y  2000  {10, 20, 30}\n",
       "c    z  3000  {10, 20, 30}\n",
       "d  new  4000            40"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_df = pd.DataFrame({'A': 'new', 'B': 4000, 'C': 40}, index = ['d'])\n",
    "df = df.append(new_df)\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "或者使用loc方法来添加行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>x</td>\n",
       "      <td>1000</td>\n",
       "      <td>{10, 20, 30}</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>y</td>\n",
       "      <td>2000</td>\n",
       "      <td>{10, 20, 30}</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>z</td>\n",
       "      <td>3000</td>\n",
       "      <td>{10, 20, 30}</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>new</td>\n",
       "      <td>4000</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>e</th>\n",
       "      <td>new2</td>\n",
       "      <td>5000</td>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      A     B             C\n",
       "a     x  1000  {10, 20, 30}\n",
       "b     y  2000  {10, 20, 30}\n",
       "c     z  3000  {10, 20, 30}\n",
       "d   new  4000            40\n",
       "e  new2  5000            50"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc['e'] = ['new2', 5000, 50]\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "处理缺漏信息，合并df和df2，使得df有一个新列E，使用join方法\n",
    "处理方法：df只接受索引已经存在的值\n",
    "为了保留df2中索引为z的值，可以提供一个参数，明确告诉Pandas如何连接两个DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   E\n",
      "a  1\n",
      "b  2\n",
      "c  3\n",
      "d  4\n",
      "z  5\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>x</td>\n",
       "      <td>1000</td>\n",
       "      <td>{10, 20, 30}</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>y</td>\n",
       "      <td>2000</td>\n",
       "      <td>{10, 20, 30}</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>z</td>\n",
       "      <td>3000</td>\n",
       "      <td>{10, 20, 30}</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>new</td>\n",
       "      <td>4000</td>\n",
       "      <td>40</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>e</th>\n",
       "      <td>new2</td>\n",
       "      <td>5000</td>\n",
       "      <td>50</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      A     B             C    E\n",
       "a     x  1000  {10, 20, 30}  1.0\n",
       "b     y  2000  {10, 20, 30}  2.0\n",
       "c     z  3000  {10, 20, 30}  3.0\n",
       "d   new  4000            40  4.0\n",
       "e  new2  5000            50  NaN"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = pd.DataFrame([1, 2, 3, 4, 5], index = ['a', 'b', 'c', 'd', 'z'], columns = ['E'])\n",
    "print(df2)\n",
    "df.join(df2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "how='outer'表示使用两个索引中所有值的并集。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>x</td>\n",
       "      <td>1000.0</td>\n",
       "      <td>{10, 20, 30}</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>y</td>\n",
       "      <td>2000.0</td>\n",
       "      <td>{10, 20, 30}</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>z</td>\n",
       "      <td>3000.0</td>\n",
       "      <td>{10, 20, 30}</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>new</td>\n",
       "      <td>4000.0</td>\n",
       "      <td>40</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>e</th>\n",
       "      <td>new2</td>\n",
       "      <td>5000.0</td>\n",
       "      <td>50</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>z</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      A       B             C    E\n",
       "a     x  1000.0  {10, 20, 30}  1.0\n",
       "b     y  2000.0  {10, 20, 30}  2.0\n",
       "c     z  3000.0  {10, 20, 30}  3.0\n",
       "d   new  4000.0            40  4.0\n",
       "e  new2  5000.0            50  NaN\n",
       "z   NaN     NaN           NaN  5.0"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.join(df2, how='outer')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "基于时间序列生成数据\n",
    "为了创建时间序列数据，需要一个时间索引\n",
    "先生成一个DatatimeIndex对象的日期序列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2016-01-01', '2016-01-02', '2016-01-03', '2016-01-04',\n",
       "               '2016-01-05', '2016-01-06', '2016-01-07', '2016-01-08'],\n",
       "              dtype='datetime64[ns]', freq='D')"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dates = pd.date_range('20160101', periods = 8)\n",
    "dates"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "基于dates创建DataFrame\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-01-01</th>\n",
       "      <td>-0.178965</td>\n",
       "      <td>0.208014</td>\n",
       "      <td>0.404006</td>\n",
       "      <td>0.044451</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-02</th>\n",
       "      <td>-1.029854</td>\n",
       "      <td>-0.921944</td>\n",
       "      <td>-0.633179</td>\n",
       "      <td>1.006232</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-03</th>\n",
       "      <td>1.602470</td>\n",
       "      <td>0.224341</td>\n",
       "      <td>0.342249</td>\n",
       "      <td>-0.211766</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-04</th>\n",
       "      <td>-1.087746</td>\n",
       "      <td>-0.635761</td>\n",
       "      <td>0.152294</td>\n",
       "      <td>0.260427</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-05</th>\n",
       "      <td>0.139927</td>\n",
       "      <td>-1.317623</td>\n",
       "      <td>-0.121151</td>\n",
       "      <td>0.075037</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-06</th>\n",
       "      <td>0.896213</td>\n",
       "      <td>1.160371</td>\n",
       "      <td>-0.350186</td>\n",
       "      <td>-0.618111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-07</th>\n",
       "      <td>-0.108617</td>\n",
       "      <td>-0.479169</td>\n",
       "      <td>0.021190</td>\n",
       "      <td>0.262808</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-08</th>\n",
       "      <td>-1.382839</td>\n",
       "      <td>-0.302839</td>\n",
       "      <td>-1.557852</td>\n",
       "      <td>0.058924</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2016-01-01 -0.178965  0.208014  0.404006  0.044451\n",
       "2016-01-02 -1.029854 -0.921944 -0.633179  1.006232\n",
       "2016-01-03  1.602470  0.224341  0.342249 -0.211766\n",
       "2016-01-04 -1.087746 -0.635761  0.152294  0.260427\n",
       "2016-01-05  0.139927 -1.317623 -0.121151  0.075037\n",
       "2016-01-06  0.896213  1.160371 -0.350186 -0.618111\n",
       "2016-01-07 -0.108617 -0.479169  0.021190  0.262808\n",
       "2016-01-08 -1.382839 -0.302839 -1.557852  0.058924"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(np.random.randn(8, 4), index = dates, columns = list('ABCD'))\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "按列求总和\n",
    "按列求均值\n",
    "按列求累积总和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A    2.276773\n",
      "B    1.976285\n",
      "C    4.123926\n",
      "D   -3.913708\n",
      "dtype: float64\n",
      "\n",
      "A    0.284597\n",
      "B    0.247036\n",
      "C    0.515491\n",
      "D   -0.489214\n",
      "dtype: float64\n",
      "\n",
      "                   A         B         C         D\n",
      "2016-01-01  0.516353  1.000439 -0.363565 -0.098910\n",
      "2016-01-02 -0.090858  0.926432 -1.879587  0.654509\n",
      "2016-01-03  0.861182  1.954360 -2.336734  0.961646\n",
      "2016-01-04  0.745453  2.167102 -1.790061  0.392028\n",
      "2016-01-05  0.353588  2.982300  0.327175  0.074795\n",
      "2016-01-06 -0.174289  0.787857  1.593075 -0.467758\n",
      "2016-01-07  0.116418  1.071671  2.243809 -1.642390\n",
      "2016-01-08  2.276773  1.976285  4.123926 -3.913708\n"
     ]
    }
   ],
   "source": [
    "print(df.sum())\n",
    "print()\n",
    "print(df.mean())\n",
    "print()\n",
    "print(df.cumsum())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Series类似于一维数组，由一组数据以及相关的数据标签（索引）组成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1\n",
       "1    4\n",
       "2    6\n",
       "3    2\n",
       "4    3\n",
       "dtype: int64"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = pd.Series([1, 4, 6, 2, 3])\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "获取值和属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 4 6 2 3]\n",
      "RangeIndex(start=0, stop=5, step=1)\n"
     ]
    }
   ],
   "source": [
    "print(s.values)\n",
    "print(s.index)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对索引进行定义，在这里，将索引定义为a，b，c，d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    1\n",
       "b    2\n",
       "c    3\n",
       "d    4\n",
       "dtype: int64"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = pd.Series([1, 2, 3, 4], index = ['a', 'b', 'c', 'd'])\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "用索引来选取Series的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "b    2\n",
      "c    3\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "print(s['a'])\n",
    "print(s[['b', 'c']])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对Series进行数据运算的时候也会保留索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b    2\n",
      "c    3\n",
      "d    4\n",
      "dtype: int64\n",
      "a     3\n",
      "b     6\n",
      "c     9\n",
      "d    12\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "print(s[s>1])\n",
    "print(s*3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Series最重要的功能之一是在不同索引中对齐数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    NaN\n",
       "b    6.0\n",
       "c    8.0\n",
       "d    NaN\n",
       "dtype: float64"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1 = pd.Series([1, 2, 3], index = ['a', 'b', 'c'])\n",
    "s2 = pd.Series([4, 5, 6], index = ['b', 'c', 'd'])\n",
    "s1 + s2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Series的索引可以通过赋值的方式直接修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['a', 'b', 'c', 'd'], dtype='object')\n",
      "Index(['w', 'x', 'y', 'z'], dtype='object')\n",
      "w    1\n",
      "x    2\n",
      "y    3\n",
      "z    4\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "print(s.index)\n",
    "s.index = ['w', 'x', 'y', 'z']\n",
    "print(s.index)\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用StatsModels\n",
    "围绕着回归模型提供操作方法，如数据访问、拟合、绘图等。\n",
    "在进行统计建模时，一般会使用StatsModels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "X =  [[0.9234262  0.73997156]\n",
      " [0.56261966 0.76571652]\n",
      " [0.35908583 0.78453812]\n",
      " [0.36910255 0.27662974]\n",
      " [0.2875534  0.9387704 ]\n",
      " [0.04714499 0.98617622]\n",
      " [0.87173221 0.19391653]\n",
      " [0.03925527 0.93893177]\n",
      " [0.48152645 0.11466809]\n",
      " [0.37325561 0.89524189]\n",
      " [0.84993176 0.6258021 ]\n",
      " [0.1074232  0.24619616]\n",
      " [0.06605766 0.30620209]\n",
      " [0.7231358  0.7502774 ]\n",
      " [0.75517109 0.78978295]\n",
      " [0.06711661 0.20247474]\n",
      " [0.39249114 0.14924443]\n",
      " [0.84540326 0.30374141]\n",
      " [0.47765781 0.54853264]\n",
      " [0.78685522 0.83443757]\n",
      " [0.91753964 0.74293289]\n",
      " [0.01432276 0.12226802]\n",
      " [0.96590614 0.52136027]\n",
      " [0.67417355 0.50043442]\n",
      " [0.05367802 0.72809186]\n",
      " [0.80039203 0.28131164]\n",
      " [0.45305862 0.19129939]\n",
      " [0.0854028  0.67990544]\n",
      " [0.69367717 0.87001664]\n",
      " [0.9531584  0.22121509]\n",
      " [0.58087448 0.41197934]\n",
      " [0.89295008 0.43442333]\n",
      " [0.79033536 0.86857426]\n",
      " [0.43230986 0.76229079]\n",
      " [0.23514885 0.39617847]\n",
      " [0.83249375 0.47329008]\n",
      " [0.79993991 0.55238151]\n",
      " [0.2055624  0.89023154]\n",
      " [0.76638047 0.90019841]\n",
      " [0.02552575 0.87990661]\n",
      " [0.8544668  0.63508373]\n",
      " [0.94604616 0.53431664]\n",
      " [0.42897329 0.02307119]\n",
      " [0.01556715 0.9496217 ]\n",
      " [0.60873101 0.49951932]\n",
      " [0.41139821 0.28526716]\n",
      " [0.14672056 0.15338852]\n",
      " [0.99078064 0.99411475]\n",
      " [0.36259465 0.20828907]\n",
      " [0.53613596 0.68031986]\n",
      " [0.64865069 0.20523969]\n",
      " [0.8769372  0.37690255]\n",
      " [0.20498956 0.10886382]\n",
      " [0.10589436 0.73262497]\n",
      " [0.77630148 0.41382479]\n",
      " [0.88461885 0.29271158]\n",
      " [0.45033354 0.49983606]\n",
      " [0.52682152 0.91419033]\n",
      " [0.71805666 0.93604158]\n",
      " [0.8734942  0.9225088 ]\n",
      " [0.59605993 0.07198214]\n",
      " [0.32271228 0.13598146]\n",
      " [0.73350289 0.9315421 ]\n",
      " [0.93104133 0.28370832]\n",
      " [0.2003454  0.33020384]\n",
      " [0.83429558 0.91253151]\n",
      " [0.83490034 0.20615327]\n",
      " [0.7620052  0.2534867 ]\n",
      " [0.4416342  0.97773304]\n",
      " [0.29972105 0.46636684]\n",
      " [0.1094612  0.2335295 ]\n",
      " [0.47573868 0.34495742]\n",
      " [0.55496809 0.05169472]\n",
      " [0.10748161 0.50311566]\n",
      " [0.63600731 0.0379926 ]\n",
      " [0.57011898 0.25262497]\n",
      " [0.86149371 0.42781825]\n",
      " [0.11459263 0.81507676]\n",
      " [0.64944138 0.39353664]\n",
      " [0.9660783  0.04340387]\n",
      " [0.42167974 0.41981074]\n",
      " [0.05349382 0.41351133]\n",
      " [0.79182363 0.58454107]\n",
      " [0.67259027 0.29208553]\n",
      " [0.02528351 0.22624567]\n",
      " [0.60420401 0.25878301]\n",
      " [0.10633746 0.58499462]\n",
      " [0.40684502 0.38971708]\n",
      " [0.07008805 0.46366415]\n",
      " [0.18297389 0.15738727]\n",
      " [0.50502112 0.9155218 ]\n",
      " [0.9281306  0.55923715]\n",
      " [0.53310428 0.28045378]\n",
      " [0.54017414 0.05212403]\n",
      " [0.16255016 0.32075742]\n",
      " [0.79749369 0.17230527]\n",
      " [0.33799324 0.38277277]\n",
      " [0.62098774 0.30856307]\n",
      " [0.72645588 0.51233866]\n",
      " [0.66726667 0.64882732]]\n",
      "X =  [[1.         0.9234262  0.73997156]\n",
      " [1.         0.56261966 0.76571652]\n",
      " [1.         0.35908583 0.78453812]\n",
      " [1.         0.36910255 0.27662974]\n",
      " [1.         0.2875534  0.9387704 ]\n",
      " [1.         0.04714499 0.98617622]\n",
      " [1.         0.87173221 0.19391653]\n",
      " [1.         0.03925527 0.93893177]\n",
      " [1.         0.48152645 0.11466809]\n",
      " [1.         0.37325561 0.89524189]\n",
      " [1.         0.84993176 0.6258021 ]\n",
      " [1.         0.1074232  0.24619616]\n",
      " [1.         0.06605766 0.30620209]\n",
      " [1.         0.7231358  0.7502774 ]\n",
      " [1.         0.75517109 0.78978295]\n",
      " [1.         0.06711661 0.20247474]\n",
      " [1.         0.39249114 0.14924443]\n",
      " [1.         0.84540326 0.30374141]\n",
      " [1.         0.47765781 0.54853264]\n",
      " [1.         0.78685522 0.83443757]\n",
      " [1.         0.91753964 0.74293289]\n",
      " [1.         0.01432276 0.12226802]\n",
      " [1.         0.96590614 0.52136027]\n",
      " [1.         0.67417355 0.50043442]\n",
      " [1.         0.05367802 0.72809186]\n",
      " [1.         0.80039203 0.28131164]\n",
      " [1.         0.45305862 0.19129939]\n",
      " [1.         0.0854028  0.67990544]\n",
      " [1.         0.69367717 0.87001664]\n",
      " [1.         0.9531584  0.22121509]\n",
      " [1.         0.58087448 0.41197934]\n",
      " [1.         0.89295008 0.43442333]\n",
      " [1.         0.79033536 0.86857426]\n",
      " [1.         0.43230986 0.76229079]\n",
      " [1.         0.23514885 0.39617847]\n",
      " [1.         0.83249375 0.47329008]\n",
      " [1.         0.79993991 0.55238151]\n",
      " [1.         0.2055624  0.89023154]\n",
      " [1.         0.76638047 0.90019841]\n",
      " [1.         0.02552575 0.87990661]\n",
      " [1.         0.8544668  0.63508373]\n",
      " [1.         0.94604616 0.53431664]\n",
      " [1.         0.42897329 0.02307119]\n",
      " [1.         0.01556715 0.9496217 ]\n",
      " [1.         0.60873101 0.49951932]\n",
      " [1.         0.41139821 0.28526716]\n",
      " [1.         0.14672056 0.15338852]\n",
      " [1.         0.99078064 0.99411475]\n",
      " [1.         0.36259465 0.20828907]\n",
      " [1.         0.53613596 0.68031986]\n",
      " [1.         0.64865069 0.20523969]\n",
      " [1.         0.8769372  0.37690255]\n",
      " [1.         0.20498956 0.10886382]\n",
      " [1.         0.10589436 0.73262497]\n",
      " [1.         0.77630148 0.41382479]\n",
      " [1.         0.88461885 0.29271158]\n",
      " [1.         0.45033354 0.49983606]\n",
      " [1.         0.52682152 0.91419033]\n",
      " [1.         0.71805666 0.93604158]\n",
      " [1.         0.8734942  0.9225088 ]\n",
      " [1.         0.59605993 0.07198214]\n",
      " [1.         0.32271228 0.13598146]\n",
      " [1.         0.73350289 0.9315421 ]\n",
      " [1.         0.93104133 0.28370832]\n",
      " [1.         0.2003454  0.33020384]\n",
      " [1.         0.83429558 0.91253151]\n",
      " [1.         0.83490034 0.20615327]\n",
      " [1.         0.7620052  0.2534867 ]\n",
      " [1.         0.4416342  0.97773304]\n",
      " [1.         0.29972105 0.46636684]\n",
      " [1.         0.1094612  0.2335295 ]\n",
      " [1.         0.47573868 0.34495742]\n",
      " [1.         0.55496809 0.05169472]\n",
      " [1.         0.10748161 0.50311566]\n",
      " [1.         0.63600731 0.0379926 ]\n",
      " [1.         0.57011898 0.25262497]\n",
      " [1.         0.86149371 0.42781825]\n",
      " [1.         0.11459263 0.81507676]\n",
      " [1.         0.64944138 0.39353664]\n",
      " [1.         0.9660783  0.04340387]\n",
      " [1.         0.42167974 0.41981074]\n",
      " [1.         0.05349382 0.41351133]\n",
      " [1.         0.79182363 0.58454107]\n",
      " [1.         0.67259027 0.29208553]\n",
      " [1.         0.02528351 0.22624567]\n",
      " [1.         0.60420401 0.25878301]\n",
      " [1.         0.10633746 0.58499462]\n",
      " [1.         0.40684502 0.38971708]\n",
      " [1.         0.07008805 0.46366415]\n",
      " [1.         0.18297389 0.15738727]\n",
      " [1.         0.50502112 0.9155218 ]\n",
      " [1.         0.9281306  0.55923715]\n",
      " [1.         0.53310428 0.28045378]\n",
      " [1.         0.54017414 0.05212403]\n",
      " [1.         0.16255016 0.32075742]\n",
      " [1.         0.79749369 0.17230527]\n",
      " [1.         0.33799324 0.38277277]\n",
      " [1.         0.62098774 0.30856307]\n",
      " [1.         0.72645588 0.51233866]\n",
      " [1.         0.66726667 0.64882732]]\n",
      "beta =  [1, 0.1, 0.5]\n",
      "y =  [2.20999136 2.17940236 2.36372058 1.75725808 1.67429335 2.31564572\n",
      " 1.74868923 1.62448461 2.05768267 1.70592341 1.97076421 1.208994\n",
      " 1.92112784 1.97538574 2.29329472 1.41714146 1.82006944 1.71298904\n",
      " 2.30560295 2.13177149 2.42720844 1.41319241 2.25021015 2.11127189\n",
      " 1.76660197 1.45174141 1.93084483 1.5521768  2.42124172 1.94471681\n",
      " 1.49552214 1.4464263  1.88239543 1.94496804 2.15364768 2.07975107\n",
      " 1.50276117 2.10021307 1.65433256 1.79127785 2.27973636 2.18382667\n",
      " 2.00464655 1.49985    2.12240513 1.76085687 1.87650745 2.120708\n",
      " 1.85033799 1.93323937 1.87924827 1.88080649 1.63032521 2.21595825\n",
      " 1.45311641 1.91880768 1.76574442 1.69189982 1.59813259 2.4932548\n",
      " 1.4747312  1.74687596 1.59152943 1.71517661 2.03374039 1.57630229\n",
      " 1.56591965 1.69337949 1.90322764 2.06646389 1.23505591 1.36728637\n",
      " 1.09334486 1.95715832 1.23658512 1.97066012 1.49193414 2.24383017\n",
      " 2.08243232 1.74983974 1.67543723 1.96042664 1.71149116 1.55846904\n",
      " 1.51546643 2.03738585 1.79726032 2.08762395 2.21018295 1.16045902\n",
      " 1.99882346 1.85553882 1.70634684 1.47514366 2.16737165 1.69853709\n",
      " 1.9376008  2.0945315  1.72720114 2.32495089]\n",
      "y.shape =  (100,)\n",
      "                            OLS Regression Results                            \n",
      "==============================================================================\n",
      "Dep. Variable:                      y   R-squared:                       0.159\n",
      "Model:                            OLS   Adj. R-squared:                  0.142\n",
      "Method:                 Least Squares   F-statistic:                     9.196\n",
      "Date:                Thu, 18 Mar 2021   Prob (F-statistic):           0.000220\n",
      "Time:                        21:29:38   Log-Likelihood:                -15.525\n",
      "No. Observations:                 100   AIC:                             37.05\n",
      "Df Residuals:                      97   BIC:                             44.87\n",
      "Df Model:                           2                                         \n",
      "Covariance Type:            nonrobust                                         \n",
      "==============================================================================\n",
      "                 coef    std err          t      P>|t|      [0.025      0.975]\n",
      "------------------------------------------------------------------------------\n",
      "const          1.5813      0.075     21.075      0.000       1.432       1.730\n",
      "x1             0.1186      0.096      1.233      0.221      -0.072       0.309\n",
      "x2             0.4111      0.101      4.074      0.000       0.211       0.611\n",
      "==============================================================================\n",
      "Omnibus:                       23.776   Durbin-Watson:                   2.333\n",
      "Prob(Omnibus):                  0.000   Jarque-Bera (JB):                5.922\n",
      "Skew:                          -0.210   Prob(JB):                       0.0518\n",
      "Kurtosis:                       1.885   Cond. No.                         5.23\n",
      "==============================================================================\n",
      "\n",
      "Warnings:\n",
      "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import statsmodels.api as sm\n",
    "\n",
    "# Generate artificail data (2 regressors + constant)\n",
    "nobs = 100\n",
    "X = np.random.random((nobs, 2))\n",
    "print('X = ', X)\n",
    "X = sm.add_constant(X)\n",
    "print('X = ', X)\n",
    "beta = [1, .1, .5]\n",
    "print('beta = ', beta)\n",
    "e = np.random.random(nobs)\n",
    "y = np.dot(X, beta) + e\n",
    "print('y = ', y)\n",
    "print('y.shape = ', y.shape)\n",
    "\n",
    "# Fit regression model\n",
    "results = sm.OLS(y, X).fit()\n",
    "\n",
    "# Inspect the results\n",
    "print(results.summary())\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
